 #@layout()
 #define content()

<span class="layui-breadcrumb">
  <a href="/">首页</a>
  <a href="/admin/dic"><cite>字典管理</cite></a>
</span>

<hr>

<div class="layui-fluid">

	<div class="layui-row">
		
		<div class="layui-col-sm5">
			<table id="dicTypeTable" lay-size="sm" lay-filter="dicTypeFilter"></table>
		</div>
		
		<div class="layui-col-sm1">&nbsp;</div> 
		
		<div class="layui-col-sm6">
			<table id="dicInfoTable" lay-size="sm" lay-filter="dicInfoFilter"></table>
		</div>
		
	</div>
</div>

#[[
<script type="text/html" id="dicTypeTableTpl">
	{{# if(d.id){ }}
		<a href="#" class="layui-table-link" lay-event="del">删除</a>
		<a href="#" class="layui-table-link" lay-event="dicDetail">字典详情</a>
	{{#	}else{ }}
		<a href="#" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="save">保存</a>
	{{#	} }}
	
</script>
]]#


#[[
<script type="text/html" id="dicInfoTableTpl">
	{{# if(d.id){ }}
		<a href="#" class="layui-table-link" lay-event="del">删除</a>
	{{#	}else{ }}
		<a href="#" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="save">保存</a>
	{{#	} }}
	
</script>
]]#

#end

#define js()

<script>

function doSaveForDicType(data,actionType){
	$.ajax({
		type:'post',
		url:'/admin/dicType/save',
		data:data,
		success:function(data){
			if(data.code){
				layer.msg('保存成功！', {
					time : 600
				}, function() {
					if(actionType=='add'){
						//编辑的时候不刷新，增强体验
						initDicType(); 
					}
				});
			}
		}
	});
}

var currDicTypeId="";
function doSaveForDicInfo(data,actionType){
	$.ajax({
		type:'post',
		url:'/admin/dicInfo/save',
		data:data,
		success:function(data){
			if(data.code){
				layer.msg('保存成功！', {
					time : 600
				}, function() {
					if(actionType=='add'){
						initDicInfo(data.data.vo.dicTypeId);
					}
				});
			}
		}
	});
}

function doDeleteForDicType(id){
	layui.layer.confirm('确认删除么？', function(index){
		$.ajax({
			type:'post',
			url:'/admin/dicType/del',
			data:"id="+id,
			success:function(data){
				if(data.code){
					layer.msg('删除成功！', {
						time : 600
					}, function() {
						initDicType();
						initDicInfo('');
					});	
				}
			}
		});
		layui.layer.close(index);
    });
}

function doDeleteForDicInfo(id){
	layui.layer.confirm('确认删除么？', function(index){
		$.ajax({
			type:'post',
			url:'/admin/dicInfo/del',
			data:"id="+id,
			success:function(data){
				if(data.code){
					layer.msg('删除成功！', {
						time : 600
					}, function() {
						initDicInfo(data.data.vo.dicTypeId);
					});	
				}
			}
		});
		layui.layer.close(index);
    });
}

function initDicType(){
	
	$.ajax({
		type:'get',
		url:'/admin/dicType/getAll',
		data:'',
		success:function(data){
			if(data.code=='0'){
				data=data.data;
				data.push({});
				
				layui.use('table',function(){
					var table=layui.table;
					
					table.render({ //其它参数在此省略
					  	elem: '#dicTypeTable', 
					  	cols:  [[ //标题栏
					  	    {field: 'name', title: '类别名称',edit:'text', width: 150},
					  	    {field: 'code', title: '类别代码',edit:'text',  width: 150},
					  	    {field: '', title: '操作', width: 120,toolbar: '#dicTypeTableTpl'},
					  	]],
					  	size: 'sm',
					  	data:data
					});
					
					table.on('tool(dicTypeFilter)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
						  var data = obj.data; //获得当前行数据
						  var layEvent = obj.event; //获得 lay-event 对应的值
						  var tr = obj.tr; //获得当前行 tr 的DOM对象
						  
						  if(layEvent === 'save'){ 
							  doSaveForDicType(data,'add');
						  } else if(layEvent === 'del'){ //删除
							  doDeleteForDicType(data.id);
						  } else if(layEvent === 'dicDetail'){ //删除
							  initDicInfo(data.id);
							  currDicTypeId=data.id;
						  }
					});
					
					
					table.on('edit(dicTypeFilter)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
						var data = obj.data; //获得当前行数据
						var field = obj.field; //获得当前行数据
						var value = obj.value; //获得当前行数据
						data[field] = value; //更新缓存中的值
						
						if(data.id){
							doSaveForDicType(data);
						}
					});
					
				});
				
			}
		}
	});
}

function initDicInfo(dicTypeId){
	
	$.ajax({
		type:'get',
		url:'/admin/dicInfo/getAll',
		data:'dicTypeId='+dicTypeId,
		success:function(data){
			if(data.code=='0'){
				data=data.data;
				data.push({});
				
				layui.use('table',function(){
					var table=layui.table;
					
					table.render({ //其它参数在此省略
					  	elem: '#dicInfoTable', 
					  	cols:  [[ //标题栏
					  	    {field: 'name', title: '字典名称',edit:'text', width: 160},
					  	    {field: 'code', title: '字典代码',edit:'text',  width: 160},
					  	    {field: 'px', title: '排序',edit:'text',  width: 80},
					  	    {field: '', title: '操作', width: 100,toolbar: '#dicInfoTableTpl'},
					  	]],
					  	size: 'sm',
					  	data:data
					});
					
					table.on('tool(dicInfoFilter)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
						  var data = obj.data; //获得当前行数据
						  var layEvent = obj.event; //获得 lay-event 对应的值
						  var tr = obj.tr; //获得当前行 tr 的DOM对象
						  if(layEvent === 'save'){ 
							  data.dicTypeId=currDicTypeId;//每次手动赋值
							  doSaveForDicInfo(data,'add');
						  } else if(layEvent === 'del'){ //删除
							  doDeleteForDicInfo(data.id);
						  }
					});
					
					
					table.on('edit(dicInfoFilter)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
						var data = obj.data; //获得当前行数据
						var field = obj.field; //获得当前行数据
						var value = obj.value; //获得当前行数据
						data[field] = value; //更新缓存中的值
						
						if(data.id){
							//如果是编辑，才进行更新
							doSaveForDicInfo(data);
						}
					});
					
				});
				
			}
		}
	});
}

initDicType();

</script>

#end


